package com.uobgroup.ews.uam.web.validation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.uobgroup.ews.uam.core.domain.Reference;
import com.uobgroup.ews.uam.core.domain.ReferenceType;
import com.uobgroup.ews.uam.core.service.ReferenceService;

@Component
public class ReferenceValidator implements Validator {

   @Autowired
   ReferenceService referenceService;
   
   @Autowired
   private ReloadableResourceBundleMessageSource messageSource;
   
   @Override
   public boolean supports(Class<?> clazz) {
      return Reference.class.getName().equals(clazz.getName()) || ReferenceType.class.getName().equals(clazz.getName()) 
            || String.class.getName().equals(clazz.getName());
   }

   @Override
   public void validate(Object target, Errors errors) {
      if(target instanceof Reference){
         Reference reference = (Reference)target;
         
         Long referenceId = reference.getId();
         String referenceName = reference.getName();//.trim();
         String referenceCode = reference.getCode();//.trim();
         ReferenceType referenceType = reference.getReferenceType();
         
         if (null == referenceName || referenceName.isEmpty()) {
            errors.rejectValue("name", "field_mandatory", new String[] { messageSource.getMessage("label_com_uobgroup_ews_uam_core_domain_reference_name", null, "", null) }, null);
         }
         if (null == referenceCode || referenceCode.isEmpty()) {
            errors.rejectValue("code", "field_mandatory", new String[] { messageSource.getMessage("label_com_uobgroup_ews_uam_core_domain_reference_code", null, "", null) }, null);
         }
         if (null == referenceType) {
            errors.rejectValue("referenceType", "field_mandatory", new String[] { messageSource.getMessage("label_com_uobgroup_ews_uam_core_domain_reference_type", null, "", null) }, null);
         }
         if (null != referenceName && !referenceName.isEmpty() && referenceName.length() > 255) {
            errors.rejectValue("name", "field_maxLength");
         }
         if (null != referenceCode && !referenceCode.isEmpty() && referenceCode.length() > 64) {
            errors.rejectValue("code", "field_maxLength");
         }
         if (null == referenceId || referenceId.longValue() == 0 ) {
            if (null != referenceName && !referenceName.isEmpty() && null != referenceService.findReferenceByName(referenceName)) {
               errors.rejectValue("name", "field_unique", new String[] { referenceName }, null);
            }
            
            if (null != referenceCode && !referenceCode.isEmpty() && null != referenceService.findReferenceByCode(referenceCode)) {
               errors.rejectValue("code", "field_unique", new String[] { referenceCode }, null);
            }
         } else {
            if (null != referenceName && !referenceName.isEmpty() && null != referenceService.findReferenceThatHasNameAndNotHasId(referenceName, referenceId)) {
               errors.rejectValue("name", "field_unique", new String[] { referenceName }, null);
            }
            
            if (null != referenceCode && !referenceCode.isEmpty() && null != referenceService.findReferenceThatHasCodeAndNotHasId(referenceCode, referenceId)) {
               errors.rejectValue("code", "field_unique", new String[] { referenceCode }, null);
            }
         }
      }
   }
}
